﻿using System;
using System.Collections.Generic;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using YJKRevitDimension.DetailIndex.Jig;

namespace YJKRevitDimension.DetailIndex.Operate
{
	internal class RangeDetailIndexOperate : DetailIndexOperateBase, IDetailIndexOperate
	{
		public bool Finished { get; set; }

		public RangeDetailIndexOperate(ExternalCommandData revit, DetailIndexParameter detailIndexPara) : base(revit, detailIndexPara)
		{
			this.Finished = false;
		}

		public bool Create(DetailIndexParameter detailIndexPara)
		{
			base.UpdateDetailIndexParameter(detailIndexPara);
			if (!this.EditFamily())
			{
				base.DeleteTempElement();
				return false;
			}
			if (!base.CreateFamilySymbol())
			{
				base.DeleteTempElement();
				return false;
			}
			return base.DeleteTempElement();
		}

		public bool EditFamily()
		{
			this.m_TargetFamilyDocPath = this.m_SourceFamilyDocPath;
			return true;
		}

		public bool CreateCirFilledRegion(Arc arc, Autodesk.Revit.DB.Document doc, Autodesk.Revit.DB.View view, ref ElementId eleId, double scale = 1.0)
		{
			bool result;
			try
			{
				CurveLoop curveLoop = new CurveLoop();
				Arc arc2 = Arc.Create(arc.Center, arc.Radius, 0.0, Math.PI, view.RightDirection, view.UpDirection);
				Arc arc3 = Arc.Create(arc.Center, arc.Radius, Math.PI, Math.PI*2, view.RightDirection, view.UpDirection);
				curveLoop.Append(arc2);
				curveLoop.Append(arc3);
				IList<CurveLoop> list = new List<CurveLoop>();
				list.Add(curveLoop);
				ElementId regionType = base.GetRegionType(doc, Shape.Circle);
				eleId = FilledRegion.Create(doc, regionType, view.Id, list).Id;
				result = true;
			}
			catch (Exception)
			{
				result = false;
			}
			return result;
		}

		public Result Operate()
		{
			base.TempElementIds.Clear();
			Transaction transaction = new Transaction(this.m_Doc, "详图索引");
			DetailIndexJig detailIndexJig = new DetailIndexJig(this.m_UIDoc, base.IfUseCustomAngle, this.Angle);
			Result result;
			try
			{
				transaction.Start();
				XYZ xyz = detailIndexJig.PickCenterPoint("请选择索引范围圆心");
				base.m_DetailIndexParameter.StartPoint = xyz;
				XYZ middlePoint = new XYZ(0.0, 0.0, 0.0);
				double num = detailIndexJig.PickRadis(xyz, ref middlePoint, "请确定圆上一点：");
				base.m_DetailIndexParameter.MiddlePoint = middlePoint;
				Curve curve = Arc.Create(xyz, num, 0.0, Math.PI*2, this.m_Doc.ActiveView.RightDirection, this.m_Doc.ActiveView.UpDirection);
				DetailCurve detailCurve = this.m_Doc.Create.NewDetailCurve(this.m_Doc.ActiveView, curve);
				base.TempElementIds.Add(detailCurve.Id);
				try
				{
					detailIndexJig.StartPoint = xyz;
					XYZ xyz2 = detailIndexJig.PickElbowPoint(xyz, "请选择转折点");
					base.m_DetailIndexParameter.ElbowPoint = xyz2;
					IntersectionResultArray intersectionResultArray;
					Line.CreateBound(xyz, xyz2).Intersect(curve, out intersectionResultArray);
					XYZ xyzpoint = intersectionResultArray.get_Item(0).XYZPoint;
					DetailCurve detailCurve2 = this.m_Doc.Create.NewDetailCurve(this.m_Doc.ActiveView, Line.CreateBound(xyzpoint, xyz2));
					base.TempElementIds.Add(detailCurve2.Id);
					XYZ xyz3 = detailIndexJig.PickEndPoint(xyz2, "请选择标注终点");
					base.m_DetailIndexParameter.EndPoint = xyz3;
					base.m_DetailIndexParameter.IsForward = base.GetForward(xyz2, xyz3);
					base.m_DetailIndexParameter.BaseLineLength = xyz3.DistanceTo(xyz2) / (double)this.m_Doc.ActiveView.Scale;
					detailIndexJig.Dispose();
				}
				catch (Exception)
				{
					detailIndexJig.Dispose();
					throw;
				}
				this.Finished = true;
				transaction.Commit();
				result = Autodesk.Revit.UI.Result.Succeeded;
			}
			catch (Exception)
			{
				transaction.RollBack();
				if (detailIndexJig != null)
				{
					detailIndexJig.Dispose();
				}
				base.DeleteTempElement();
				this.Finished = false;
				result = Autodesk.Revit.UI.Result.Cancelled;
			}
			return result;
		}
	}
}
